package com.dk.testcase;

import com.dk.data.DataUtil;
import com.dk.domain.*;
import com.dk.exception.ExcelDataException;
import com.dk.testflow.ApiAutoTestChainConfig;
import com.dk.util.config.ReadConfig;
import com.dk.util.file.YamlUtil;
import com.dk.util.jdbc.JdbcUtil;
import com.dk.util.log.Log;
import io.qameta.allure.Allure;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class BaseCase {
    //接口自动化测试用例集
    public static Map<String,ApiAutoTestEntity> apiAutoTestEntityMap;
    //动作字典
    public static Map<String,ActionEntity> actionEntityMap;
    //全局变量字典
    public static Map<String, Object> varEntityMap;
    //数据库信息字典
    public static Map<String,Map> dbConfigMap;
    //日志
    public static StringBuffer log = new StringBuffer();
    static{
        Log.info("---【数据加载】:开始加载测试数据");
        //加载数据库信息
        dbConfigMap = YamlUtil.loadYamlData(ReadConfig.JDBC_CONFIGPATH);
        //加载接口数据
        List<ApiEntity> apiEntityList = DataUtil.getTestDataInSheet(ReadConfig.DATA_PATH, ReadConfig.DATA_SHEET_API, ApiEntity.class);
        //加载用例数据
        List<CaseEntity> caseEntityList = DataUtil.getTestDataInSheet(ReadConfig.DATA_PATH, ReadConfig.DATA_SHEET_CASE, CaseEntity.class);

        //获取指定文件夹下所有文件指定sheet的数据,转换为指定格式的map
        actionEntityMap = DataUtil
                .getTestDataInSheet(ReadConfig.DATA_PATH, ReadConfig.DATA_SHEET_ACTION, ActionEntity.class)
                .stream().collect(Collectors.toMap(ActionEntity::getActionNum,ActionEntity->ActionEntity));
        varEntityMap = DataUtil
                .getTestDataInSheet(ReadConfig.DATA_PATH, ReadConfig.DATA_SHEET_VAR, VarEntity.class)
                .stream().collect(Collectors.toMap(VarEntity::getKey,VarEntity::getValue));
        apiAutoTestEntityMap = DataUtil
                .getApiAutoTestSet(apiEntityList,caseEntityList)
                .stream().collect(Collectors.toMap(ApiAutoTestEntity::getCaseNum,ApiAutoTestEntity->ApiAutoTestEntity));
        Log.info("---【数据加载】:测试数据加载成功");
    }

}
